package huichun_r

import (
	"fmt"
	"frozen-go-mini/common/domain"
	"frozen-go-mini/common/mycontext"
	"frozen-go-mini/domain/huichun_m"
	"frozen-go-mini/req"
	"frozen-go-mini/resp"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"sort"
	"strings"
)

type Product struct {
	Id        uint64   `json:"id"`         // id
	Name      string   `json:"name"`       // 标题
	Desc      string   `json:"desc"`       // desc
	Pic       string   `json:"pic"`        // 图片
	Tags      []string `json:"tags"`       // 标签
	Price     string   `json:"price"`      // 价格
	Sales     int64    `json:"sales"`      // 销量
	Stock     int64    `json:"stock"`      // 库存
	IsCollect bool     `json:"is_collect"` // 是否收藏
}

type MarketListData struct {
	Spu      string    `json:"spu"`      // spu
	Products []Product `json:"products"` // 商品信息
}

type MarketListResp struct {
	Items []MarketListData `json:"items"`
}

// @Tags 挥春
// @Summary 商品列表
// @Param Authorization header string true "token"
// @Success 200 {object} MarketListResp
// @Router /v1.0/huichun/market/list [get]
func MarketList(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	model := domain.CreateModelContext(myCtx)
	list := huichun_m.GetMarketList(model)
	var response MarketListResp
	userId, _ := req.GetUserId(c)
	myCollect := huichun_m.GetIsMyCollect(model, userId)
	for spu, products := range list {
		data := MarketListData{Spu: spu}
		for _, product := range products {
			data.Products = append(data.Products, Product{
				Id:        product.ID,
				Name:      product.Sku,
				Pic:       product.Pics,
				Tags:      strings.Split(product.Tags, ","),
				Price:     fmt.Sprintf("%.2f", float64(product.DiscPrice)/100),
				IsCollect: myCollect[product.ID],
			})
		}
		response.Items = append(response.Items, data)
	}
	sort.Slice(response.Items, func(i, j int) bool {
		return response.Items[i].Spu < response.Items[j].Spu
	})
	resp.ResponseOk(c, response)
	return myCtx, nil
}

// @Tags 挥春
// @Summary 商品明细
// @Param Authorization header string true "token"
// @Success 200 {object} Product
// @Router /v1.0/huichun/market/detail/:id [get]
func MarketDetail(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	model := domain.CreateModelContext(myCtx)
	userId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}
	product := huichun_m.GetMarketDetail(model, cast.ToUint64(c.Param("id")))
	myCollect := huichun_m.GetIsMyCollect(model, userId)
	var response = Product{
		Id:        product.ID,
		Name:      product.Sku,
		Desc:      product.Desc,
		Pic:       product.Pics,
		Tags:      strings.Split(product.Tags, ","),
		Price:     fmt.Sprintf("%.2f", float64(product.DiscPrice)/100),
		Sales:     product.Sales,
		Stock:     product.Stock,
		IsCollect: myCollect[product.ID],
	}
	if product.ID > 0 {
		if err := huichun_m.AddUserFootprint(model, huichun_m.UserFootprint{
			UserId:    userId,
			ProductId: product.ID,
		}); err != nil {
			model.Log.Errorf("AddUserFootprint fail:%v", err)
		}
	}

	resp.ResponseOk(c, response)
	return myCtx, nil
}

type HouseSpecialty struct {
	ProductId uint64 `json:"product_id"` // 商品id
	Name      string `json:"name"`       // 标题
	Desc      string `json:"desc"`       // 描述
	Banner    string `json:"banner"`     // banner
	Tag       string `json:"tag"`        // 标签
	Price     string `json:"price"`      // 价格
	Sales     int64  `json:"sales"`      // 销量
	IsCollect bool   `json:"is_collect"` // 是否收藏
}

// @Tags 挥春
// @Summary 本店招牌
// @Param Authorization header string true "token"
// @Success 200 {object} []HouseSpecialty
// @Router /v1.0/huichun/market/specialty [get]
func MarketSpecialty(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	model := domain.CreateModelContext(myCtx)
	product := huichun_m.GetHuichunHouseSpecialty(model)
	var response []HouseSpecialty
	userId, _ := req.GetUserId(c)
	myCollect := huichun_m.GetIsMyCollect(model, userId)
	for _, p := range product {
		var tag string
		tags := strings.Split(p.Tags, ",")
		if len(tags) > 0 {
			tag = tags[0]
		}
		response = append(response, HouseSpecialty{
			ProductId: p.ID,
			Name:      p.Sku,
			Desc:      p.Desc,
			Banner:    p.Pics,
			Tag:       tag,
			Price:     fmt.Sprintf("%.2f", float64(p.Price)/100),
			Sales:     p.Sales,
			IsCollect: myCollect[p.ID],
		})
	}

	resp.ResponseOk(c, response)
	return myCtx, nil
}
